package com.basic.library.adapter;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;

import java.util.List;

/**
 * @author jiangyun
 * @version 创建时间：2017/7/24 09:35
 */
public class FragmentAdapter<T extends Fragment> extends FragmentStatePagerAdapter {
    private List<T> mFragments;
    private List<String> mTitles;

    public FragmentAdapter(FragmentManager fm, List<T> fragments, List<String> titles) {
        super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        mFragments = fragments;
        mTitles = titles;
    }

    @Override
    public Fragment getItem(int position) {
        return mFragments.get(position);
    }

    @Override
    public int getCount() {
        return mFragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mTitles.get(position);
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        if (object instanceof Fragment) {

            if (mFragments.contains(object)) {
                return mFragments.indexOf(object);
            } else {
                return POSITION_NONE;
            }
        }
        return super.getItemPosition(object);
    }

}